/**
 * 简单的事件总线实现，用于组件间通信
 * @author Cascade
 */
import { ref, watch } from 'vue'

// 创建一个简单的事件总线
class EventBus {
  private events: Record<string, Function[]> = {}

  // 订阅事件
  on(event: string, callback: Function) {
    if (!this.events[event]) {
      this.events[event] = []
    }
    this.events[event].push(callback)
  }

  // 发布事件
  emit(event: string, data?: any) {
    if (this.events[event]) {
      this.events[event].forEach(callback => callback(data))
    }
  }

  // 取消订阅
  off(event: string, callback?: Function) {
    if (!callback) {
      delete this.events[event]
    } else if (this.events[event]) {
      this.events[event] = this.events[event].filter(cb => cb !== callback)
    }
  }
}

// 创建并导出事件总线实例
export default new EventBus()
